package com.qiantu.phone.widget.setting;

import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceLedSetBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.activity.DeviceLedSetActivity;

import okhttp3.Call;

/**
 * @description:
 * @author: 86181
 * @time: 2023/1/5 15:04
 */
public class DeviceSettingLightShow extends SettingBar implements View.OnClickListener{
    private DeviceBean mDeviceBean;
    private AppActivity mContext;
    private String deviceSerialNo;

    private FragmentManager mFragmentManager;
    private Fragment deviceFragment;
    private View deviceViewContainer;
    private TranslateAnimation mShowTranslateAnimation;

    public DeviceSettingLightShow(AppActivity context, DeviceBean deviceBean, String title) {
        this(context,null,deviceBean,title);
    }

    public DeviceSettingLightShow(AppActivity context, AttributeSet attrs,DeviceBean deviceBean, String title) {
        this(context, attrs,0,deviceBean,title);
    }

    public DeviceSettingLightShow(AppActivity context, AttributeSet attrs, int defStyleAttr,DeviceBean deviceBean, String title) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style,deviceBean,title);
    }

    public DeviceSettingLightShow(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes,DeviceBean deviceBean, String title) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDeviceBean = deviceBean;
        mContext = context;
        deviceViewContainer = context.findViewById(R.id.device_view_container);
        setLeftText(title);
        setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        deviceSerialNo = deviceBean.getDeviceSerialNo();
        setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        LLHttpManager.getDeviceLedSet(mContext,deviceSerialNo,new HttpCallback<HttpData<DeviceLedSetBean>>(mContext){
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<DeviceLedSetBean> result) {
                super.onSucceed(result);
                DeviceLedSetBean data = result.getData();
                Intent intent = new Intent(mContext, DeviceLedSetActivity.class);
                intent.putExtra("deviceSerialNo",data.getDeviceSerialNo());
                intent.putExtra("houseSerialNo",mDeviceBean.getHouseSerialNo());
                intent.putExtra("startsleeptime",data.getStateData().getSettings().getStartsleeptime());
                intent.putExtra("endsleeptime",data.getStateData().getSettings().getEndsleeptime());
                intent.putExtra("waitsleeptime",data.getStateData().getSettings().getWaitsleeptime());
                intent.putExtra("enablesleep",data.getStateData().getSettings().getEnablesleep());
                intent.putExtra("isSyncAll",data.getSyncAll());
                mContext.startActivity(intent);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }
}
